package CustomCMS
{
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.TextEvent;
	import flash.ui.Keyboard;
	
	import mx.controls.TextArea;
	import mx.controls.textClasses.TextRange;

	public class TextEditor extends TextArea
	{
		public var tabCapture:Boolean = false;

		public function TextEditor()
		{
			super();
			this.addEventListener(KeyboardEvent.KEY_DOWN, KeyListener);
		}
		
		protected function KeyListener(event:KeyboardEvent) : void
		{
			//trace("shiftKey="+event.shiftKey+", ctrlKey="+event.ctrlKey+", altKey="+event.altKey+", capsLock="+Keyboard.capsLock+", numLock="+Keyboard.numLock+", keyCode="+event.keyCode+", charCode="+event.charCode+", keyLocation="+event.keyLocation);
			if (event.keyCode == Keyboard.TAB && (this.tabCapture || event.ctrlKey || event.altKey || Keyboard.capsLock))
			{
				event.preventDefault();
				callLater(this.insertTab);
   			}
  		}
		
		private function insertTab() : void
		{
			this.setFocus();
 			this.dispatchEvent(new TextEvent(TextEvent.TEXT_INPUT, false, false, '\t'));
 			var tr:TextRange = new TextRange(this, true, this.selectionBeginIndex, this.selectionEndIndex);
 			tr.text = '\t';
 			this.textField.setSelection(tr.endIndex, tr.endIndex);
            this.dispatchEvent(new Event(Event.CHANGE));
		}
	}
}